package com.sgb.kjwl.viewmodel.self;

import com.sgb.kjwl.api.APIManager;
import com.sgb.link.entity.InvoiceManagementEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.InvoiceManagementActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *
 */
public class InvoiceManagementModel extends BaseViewModel {
    private InvoiceManagementActivity activity;
    private SelfRepertory repertory;
    private String companyNo;

    public InvoiceManagementModel(InvoiceManagementActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    public void getBillList(String companyNo) {
        this.companyNo = companyNo;
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().billList(companyNo).enqueue(new Callback<BaseEntity<ListResponse<List<InvoiceManagementEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<InvoiceManagementEntity>>>> call, Response<BaseEntity<ListResponse<List<InvoiceManagementEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) return;
                if (response.body().getState().equals("ok")) {
                    activity.showInfo(response.body().getData().getList());
                } else {
                    assert response.body() != null;
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<InvoiceManagementEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void deleteBill(String id) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().deleteBill(id).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    MToast.showToast(activity, response.body().getData().toString());
                    getBillList(companyNo);
                }
                assert response.body() != null;
                MToast.showToast(activity, response.body().getMsg());

            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 修改当前id的收货地址
     */
    public void addBill(InvoiceManagementEntity invoiceManagementEntity) {
        DialogHelper.showProgressDialog(activity, null, "数据提交中...", 0, false, null).setCanceledOnTouchOutside(true);
        APIManager.getInstance().getUserAPI().addBill(invoiceManagementEntity).enqueue(new Callback<BaseEntity<InvoiceManagementEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<InvoiceManagementEntity>> call, Response<BaseEntity<InvoiceManagementEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null) {
                    MToast.showToast(activity, response.body().getMsg());
                    if (response.body().getState().equals("ok")) {
                        getBillList(companyNo);
                    } else {
                        assert response.body() != null;
                        MToast.showToast(activity, response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<InvoiceManagementEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
}
